/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-12-12 17:24:00   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.cloud.domain.ClientContact;
import com.zx.cloud.domain.ClientInfo;
import com.zx.cloud.domain.ClientLoanOrder;
import com.zx.cloud.domain.ClientRisk;
import com.zx.cloud.domain.Dict;
import com.zx.cloud.persistence.ClientContactMapper;
import com.zx.cloud.persistence.ClientInfoMapper;
import com.zx.cloud.persistence.ClientRiskMapper;
import com.zx.cloud.persistence.CrudMapper;
import com.zx.cloud.persistence.DictMapper;
import com.zx.cloud.security.Base64;

/**
 * fengkongshuju
 */
@Service
public class ClientRiskService extends CrudServiceImpl<ClientRisk>{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientRiskService.class);
    
  	@Autowired
    public ClientRiskService(CrudMapper<ClientRisk> mapper) {
        super(mapper);
    }
  	@Autowired
  	private ClientRiskMapper   clientRiskMapper;
    @Autowired
    private ClientInfoMapper clientInfoMapper;
	@Autowired
	private DictMapper dictMapper;
	
	@Autowired
	 private ClientContactMapper clientContactMapper;
  	
  	
  	public  ClientRisk  getClientRiskSm(ClientRisk clientRisk){
  		ClientRisk  newClientRisk=new ClientRisk();
  		newClientRisk.setClientId(clientRisk.getClientId());
  		newClientRisk.setRiskChannel(2);
  		newClientRisk.setRiskType(2);
  		List<ClientRisk> clientRisks=clientRiskMapper.getClientRisk(newClientRisk);
  		if(clientRisks.size()>0){
  			return clientRisks.get(0);
  		}else{
  			return null;
  		}
  		
  	}

  	public  ClientInfo  getClientInfo(long clientId){
  		ClientInfo clientInfo = clientInfoMapper.findClientById(clientId);
  		
  		return  clientInfo;
  	}
  	
  	public  int  getClientContactLength(long clientId){
  		List<ClientContact> clientContacts = clientContactMapper.findContactByClientId(Long.toString(clientId));
  		int length=0;
  		if(null != clientContacts && clientContacts.size() > 0){
  			String contacts = clientContacts.get(clientContacts.size()-1).getContacts();
				if(StringUtils.isNotEmpty(contacts)){
					byte[] bytes = Base64.decode(contacts);//解密
  					String contactsData=new String(bytes);
  					JSONArray jsonArray = new JSONArray(contactsData);
  					length=jsonArray.length();
				}
  		}else {
  			length=0;
		}
  		LOGGER.info("==========通讯录信息=========="+clientId+"=========="+length+"==========");
  		return  length;
  	}
  	
  	
  	public  ClientLoanOrder  getClientLoanOrder(long clientId){
  		ClientLoanOrder  cLoanOrder=new ClientLoanOrder();
  		cLoanOrder.setClientId(clientId);
  		//会员信息
  		ClientInfo clientInfo = getClientInfo(clientId);
  		//会员通讯信息
  		int length=getClientContactLength(clientId);
  		/**相关利率*/
  		BigDecimal interestRate=new BigDecimal(0);
  		/**14天服务费*/
		BigDecimal serviceRate14=new BigDecimal(0);
		/**7天服务费*/
		BigDecimal serviceRate7=new BigDecimal(0);
		/**逾期*/
		BigDecimal overdueRate=new BigDecimal(0);
		Dict dict=new Dict();
		dict.setParentCode("R0001");
		List<Dict> dicts=dictMapper.getDitByParentCode(dict);
		for (int i = 0; i < dicts.size(); i++) {
			if("R00001".equals(dicts.get(i).getCode())){
				interestRate=interestRate.add(new BigDecimal(dicts.get(i).getValue()));
			}
			if("R00002".equals(dicts.get(i).getCode())){
				serviceRate14=serviceRate14.add(new BigDecimal(dicts.get(i).getValue()));
			}
			if("R00003".equals(dicts.get(i).getCode())){
				overdueRate=overdueRate.add(new BigDecimal(dicts.get(i).getValue()));
			}
			if("R00004".equals(dicts.get(i).getCode())){
				serviceRate7=serviceRate7.add(new BigDecimal(dicts.get(i).getValue()));
			}
		}
		int  age=clientInfo.getAge();
		LOGGER.info("=====================================================");
		LOGGER.info("=========="+clientId+"=========="+age+"=========="+length+"==========");
		LOGGER.info("=====================================================");
		if(age<=18 || length <15){
			return  cLoanOrder;
		}else if(age >40){
			return  cLoanOrder;
		}else if(age >19 && age <30 && length>50 ){
			/**借款金额*/
			BigDecimal loanAmount=new BigDecimal(1000);
			cLoanOrder.setLoanAmount(loanAmount);
			cLoanOrder.setLoanTerm(14);
			/**利息计算*/
			BigDecimal loanInterest=new BigDecimal(0);
			loanInterest=loanInterest.add(loanAmount.multiply( interestRate ).multiply( new BigDecimal(14 ) ));
			cLoanOrder.setInterestRate(interestRate);
			cLoanOrder.setLoanInterest(loanInterest);
			/**服务费计算*/
			BigDecimal serviceCharge=new BigDecimal(0);
			serviceCharge=serviceCharge.add(loanAmount.multiply( serviceRate14 ).multiply( new BigDecimal(14 ) ));
			cLoanOrder.setServiceRate(serviceRate14);
			cLoanOrder.setServiceCharge(serviceCharge);
			/**逾期利率*/
			cLoanOrder.setOverdueRate(overdueRate);
			/**费用总和*/
			BigDecimal feeTotal=loanInterest.add(serviceCharge);
			cLoanOrder.setFeeTotal(feeTotal);
			/**到账金额*/
			BigDecimal actualAmount=loanAmount.subtract(feeTotal);
			cLoanOrder.setActualAmount(actualAmount);
			return  cLoanOrder;
		}else if(age >31 && age <40){
			/**借款金额*/
			BigDecimal loanAmount=new BigDecimal(1000);
			cLoanOrder.setLoanAmount(loanAmount);
			cLoanOrder.setLoanTerm(7);
			/**利息*/
			BigDecimal loanInterest=new BigDecimal(0);
			loanInterest=loanInterest.add(loanAmount.multiply( interestRate ).multiply( new BigDecimal(7 ) ));
			cLoanOrder.setInterestRate(interestRate);
			cLoanOrder.setLoanInterest(loanInterest);
			/**服务费*/
			BigDecimal serviceCharge=new BigDecimal(0);
			serviceCharge=serviceCharge.add(loanAmount.multiply( serviceRate7 ).multiply( new BigDecimal(7 ) ));
			cLoanOrder.setServiceRate(serviceRate7);
			cLoanOrder.setServiceCharge(serviceCharge);
			/**费用总和*/
			BigDecimal feeTotal=loanInterest.add(serviceCharge);
			cLoanOrder.setFeeTotal(feeTotal);
			/**到账金额*/
			BigDecimal actualAmount=loanAmount.subtract(feeTotal);
			cLoanOrder.setActualAmount(actualAmount);
			return  cLoanOrder;
		}else if(length >=15 && length <=50){
			/**借款金额*/
			BigDecimal loanAmount=new BigDecimal(1000);
			cLoanOrder.setLoanAmount(loanAmount);
			cLoanOrder.setLoanTerm(7);
			/**利息*/
			BigDecimal loanInterest=new BigDecimal(0);
			loanInterest=loanInterest.add(loanAmount.multiply( interestRate ).multiply( new BigDecimal(7 ) ));
			cLoanOrder.setInterestRate(interestRate);
			cLoanOrder.setLoanInterest(loanInterest);
			/**服务费*/
			BigDecimal serviceCharge=new BigDecimal(0);
			serviceCharge=serviceCharge.add(loanAmount.multiply( serviceRate7 ).multiply( new BigDecimal(7 ) ));
			cLoanOrder.setServiceRate(serviceRate7);
			cLoanOrder.setServiceCharge(serviceCharge);
			/**费用总和*/
			BigDecimal feeTotal=loanInterest.add(serviceCharge);
			cLoanOrder.setFeeTotal(feeTotal);
			/**到账金额*/
			BigDecimal actualAmount=loanAmount.subtract(feeTotal);
			cLoanOrder.setActualAmount(actualAmount);
			return  cLoanOrder;
		}
		return  cLoanOrder;
  	}
}
